package com.beta.pay.yeepay.request;

import com.beta.pay.yeepay.enums.DivideType;
import org.apache.commons.collections.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by vector on 2016/12/12.
 */
public class DivideRequest extends BaseRequest {

    private static final long serialVersionUID = -4375875924603210083L;

    /**
     * 商户订单号：已完成支付的订单号
     */
    private String orderRequestId;

    /**
     * 分账类型
     */
    private DivideType divideType = DivideType.AMOUNT;

    /**
     * 回调地址：分账之后回到通知商户
     */
    private String callbackUrl;

    /**
     * 分账信息：
     * divideinfo详解：
     *  1、当订单按比例分账时，格式：10012465180:0.4|10012465181:0.3
     *  2、当订单按金额分账时，格式：10012465180:AMOUNT13.25|10012465181:AMOUNT20；该种方 式的分账金额最多精确到小数点后两位。
     *  3、分账金额的计算：A 商户编号:0.4|B商户编号:0.055： 金额计算：如订单金额为 100，费率为1%，则分给A的金额=（100-100*1%）*0.4 = 39.6； 分给 B的金额=（100-100*1%）*0.055 = 5.45【5.445四舍五入为 5.45元】。
     *  4、分账按固定金额分账是两位小数，比例分账没有小数位数限制。
     */
    private List<DivideInfo> divideInfos;

    public DivideRequest(String requestId) {
        super(requestId);
    }

    public String getOrderRequestId() {
        return orderRequestId;
    }

    public DivideRequest setOrderRequestId(String orderRequestId) {
        this.orderRequestId = orderRequestId;
        return this;
    }

    public DivideType getDivideType() {
        return divideType;
    }

    public DivideRequest setDivideType(DivideType divideType) {
        this.divideType = divideType;
        return this;
    }

    public String getCallbackUrl() {
        return callbackUrl;
    }

    public DivideRequest setCallbackUrl(String callbackUrl) {
        this.callbackUrl = callbackUrl;
        return this;
    }

    public List<DivideInfo> getDivideInfos() {
        return divideInfos;
    }

    public DivideRequest setDivideInfos(List<DivideInfo> divideInfos) {
        this.divideInfos = divideInfos;
        return this;
    }

    public String divideInfoString() {
        if (CollectionUtils.isEmpty(this.divideInfos)) {
            return "";
        }

        if (DivideType.RATIO.equals(divideType)) {
            return this.divideInfos.stream().map(divideInfo -> {
                return divideInfo.getLedgerCode() + ":" + divideInfo.getRatio();
            }).collect(Collectors.joining("|"));
        } else if (DivideType.AMOUNT.equals(divideType)) {
            return this.divideInfos.stream().map(divideInfo -> {
                return divideInfo.getLedgerCode() + ":AMOUNT" + divideInfo.getAmount().setScale(2, RoundingMode.HALF_EVEN).toString();
            }).collect(Collectors.joining("|"));
        }
        return "";
    }

    public static class DivideInfo {

        /**
         * 分账的账号
         */
        private String ledgerCode;

        /**
         * 分账的比例
         */
        private BigDecimal ratio;

        /**
         * 分账金额
         */
        private BigDecimal amount;

        private DivideInfo(String ledgerCode, BigDecimal ratio, BigDecimal amount) {
            this.ledgerCode = ledgerCode;
            this.ratio = ratio;
            this.amount = amount;
        }

        public static DivideInfo ofRatio(String ledgerCode, BigDecimal ratio) {
            return new DivideInfo(ledgerCode, ratio, null);
        }

        public static DivideInfo ofAmount(String ledgerCode, BigDecimal amount) {
            return new DivideInfo(ledgerCode, null, amount);
        }

        public String getLedgerCode() {
            return ledgerCode;
        }

        public BigDecimal getRatio() {
            return ratio;
        }

        public BigDecimal getAmount() {
            return amount;
        }
    }

}
